
import json, os, sys, time, random, _thread

def _app_py(request_, response_, route_args_):
	with open('web/index_py.html') as fp:
		content = fp.read()
	app = route_args_['app'].strip()
	if app != '':
		content = content.replace("/web/main.py", f"/web/{app}.py")
	return response_.write_response_OK_(content_type_='text/html', content_=content, charset_='UTF-8')

def _app(request_, response_, route_args_):
	app = route_args_['app'].strip()
	if app=='':
		route_args_['app'], app = 'main', 'main'
	if os.path.isfile(f'web/{app}.py'):
		return _app_py(request_, response_, route_args_)
	return response_.write_response_error_(400, f'不存在 web/{app}.py')

rpc_registry = {}
def _handle_rpc(data):
	func_name = data['func_name']
	argv = data['argv']
	kwargs = data['kwargs']
	api_key = data['api_key']
	if func_name not in rpc_registry:
		return {'error': f'没有该服务: {func_name}'}
	if len(rpc_registry[func_name])==2:
		api_key_gt, func = rpc_registry[func_name]
		rpc_registry[func_name] = (api_key_gt, func, _thread.allocate_lock())
	api_key_gt, func, lock = rpc_registry[func_name]
	if api_key_gt is not None and api_key_gt!=api_key:
		return {'error': 'api_key' if api_key is None else f'错误的 API_KEY: {api_key}'}
	else:
		try:
			lock.acquire()
			res = func(*argv, **kwargs)
		finally:
			lock.release()
		return res

def _ajax(request_, response_, route_args_):
	global rpc_registry
	params_ = request_.params_
	assert 'data' in params_, '服务请求参数中缺少 data'
	data = json.loads(params_['data'])
	try:
		res = _handle_rpc(data)
	except Exception as ex:
		from serv.lib.http_ import http_handle_ex_
		res = {'error': http_handle_ex_(ex)}
	json_ = json.dumps(res)
	return response_.write_response_JSON_OK_(json_)

http_ = None
def _start_server(port_, max_threads_):
	from .lib.http_ import Http_
	global http_
	assert http_ is None
	http_ = Http_(ip_='0.0.0.0', port_=port_, web_path_='web', max_threads_=max_threads_)
	http_.add_route_('/ajax', _ajax, 'POST')
	http_.add_route_('/{app}', _app, 'GET')
	http_.add_route_('/{app}', _app, 'POST')
	http_.start_()

def _stop_server():
	http_.stop_()

# from . import css
# css.download_mdl_themes()

def rpc_auto_move(board_key):
	from web.py_lib import chess
	board = chess.board_from_key(board_key)
	# from web.py_lib.min_max import auto_move
	from web.py_lib.alpha_beta import auto_move
	return auto_move(board)

def rpc_load_csv(filename):
	assert filename.endswith('.csv')
	history = []
	with open(filename) as fp:
		for line in fp:
			line = line.split(',')
			if len(line)==0: continue
			assert len(line)==7
			history.append([int(m) for m in line[2:6]])
	return history

# 登记函数 RPC auto_move
rpc_registry['rpc_auto_move'] = [None,rpc_auto_move]
rpc_registry['rpc_load_csv'] = [None,rpc_load_csv]

port = int(sys.argv[1]) if len(sys.argv)>=2 else 80

print(f'网页服务器端口: {port}')
_start_server(port, 100)
print('网页服务器已关闭')
